import { MigrationInterface, QueryRunner } from "typeorm";

export class CreateMonitoringTables1700000000000 implements MigrationInterface {
  name = 'CreateMonitoringTables1700000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建页面访问表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS page_visits (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        page_url VARCHAR(500),
        page_title VARCHAR(200),
        referrer VARCHAR(500),
        browser VARCHAR(100),
        os VARCHAR(100),
        ip_address VARCHAR(45),
        user_agent TEXT,
        visit_time DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_visit_time (visit_time),
        INDEX idx_page_url (page_url(255))
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建用户事件表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS user_events (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        event_type VARCHAR(50),
        element_tag VARCHAR(50),
        element_text VARCHAR(500),
        element_class VARCHAR(200),
        element_id VARCHAR(100),
        x_coordinate INT,
        y_coordinate INT,
        page_url VARCHAR(500),
        browser VARCHAR(100),
        os VARCHAR(100),
        ip_address VARCHAR(45),
        user_agent TEXT,
        event_time DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_event_time (event_time),
        INDEX idx_event_type (event_type),
        INDEX idx_page_url (page_url(255))
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建错误日志表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS error_logs (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        error_type VARCHAR(50),
        error_message TEXT,
        error_stack TEXT,
        source_file VARCHAR(500),
        line_number INT,
        column_number INT,
        page_url VARCHAR(500),
        browser VARCHAR(100),
        os VARCHAR(100),
        ip_address VARCHAR(45),
        user_agent TEXT,
        occurred_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_occurred_at (occurred_at),
        INDEX idx_error_type (error_type),
        INDEX idx_page_url (page_url(255))
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建性能指标表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS performance_metrics (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        page_url VARCHAR(500),
        page_title VARCHAR(200),
        navigation_start BIGINT,
        unload_event_start BIGINT,
        unload_event_end BIGINT,
        redirect_start BIGINT,
        redirect_end BIGINT,
        fetch_start BIGINT,
        domain_lookup_start BIGINT,
        domain_lookup_end BIGINT,
        connect_start BIGINT,
        connect_end BIGINT,
        secure_connection_start BIGINT,
        request_start BIGINT,
        response_start BIGINT,
        response_end BIGINT,
        dom_loading BIGINT,
        dom_interactive BIGINT,
        dom_content_loaded_event_start BIGINT,
        dom_content_loaded_event_end BIGINT,
        dom_complete BIGINT,
        load_event_start BIGINT,
        load_event_end BIGINT,
        first_paint BIGINT,
        first_contentful_paint BIGINT,
        largest_contentful_paint BIGINT,
        first_input_delay BIGINT,
        cumulative_layout_shift DECIMAL(10,2),
        browser VARCHAR(100),
        os VARCHAR(100),
        ip_address VARCHAR(45),
        user_agent TEXT,
        recorded_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_recorded_at (recorded_at),
        INDEX idx_page_url (page_url(255))
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建API请求表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS api_requests (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        api_url VARCHAR(500),
        http_method VARCHAR(10),
        status_code INT,
        duration INT,
        success BOOLEAN,
        request_time DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_request_time (request_time),
        INDEX idx_api_url (api_url(255)),
        INDEX idx_status_code (status_code)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建用户会话表
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS user_sessions (
        id VARCHAR(36) PRIMARY KEY DEFAULT UUID(),
        user_id VARCHAR(64),
        session_start DATETIME DEFAULT CURRENT_TIMESTAMP,
        session_end DATETIME,
        total_duration INT,
        page_views INT DEFAULT 0,
        events_count INT DEFAULT 0,
        errors_count INT DEFAULT 0,
        ip_address VARCHAR(45),
        user_agent TEXT,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_created_at (created_at),
        INDEX idx_session_start (session_start)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS user_sessions`);
    await queryRunner.query(`DROP TABLE IF EXISTS api_requests`);
    await queryRunner.query(`DROP TABLE IF EXISTS performance_metrics`);
    await queryRunner.query(`DROP TABLE IF EXISTS error_logs`);
    await queryRunner.query(`DROP TABLE IF EXISTS user_events`);
    await queryRunner.query(`DROP TABLE IF EXISTS page_visits`);
  }
}